<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
/*
 * 获取客户端IP
 */
$ip = get_client_ip();
echo '你的客户端IP: ' . $ip . "<br/>\n";
echo '<hr/>';
echo '$_SERVER["HTTP_CLIENT_IP"] = ' . (isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : '') . "<br/>\n";
echo '$_SERVER["HTTP_X_FORWARDED_FOR"] = ' . (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '') . "<br/>\n";
echo '$_SERVER["REMOTE_ADDR"] = ' . (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '') . "<br/>\n";

//返回客户端IP地址
function get_client_ip()
{
    if (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP'] && strcasecmp($_SERVER['HTTP_CLIENT_IP'], "unknown"))
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], "unknown"))
    {
        $ip = strtok($_SERVER['HTTP_X_FORWARDED_FOR'], ',');
    }
    elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
    {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    else
    {
        $ip = "unknown";
    }
    return $ip;
}
